﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace server.filters
{
    public class DecodeFrameFilter : NetFilter
    {

        private byte[] frame ;

        private int frameIndex = 0;

        private int status = 0;

        private int frameLength = 0;

        public void invoke(object buffer, InvocationChain invocationChain)
        {

            if (!(buffer is byte[])) { 
                invocationChain.invoke(buffer);
            }

            

            byte[] byteBuffer = (byte[])buffer;

                

            for (int index=0;index<  byteBuffer.Length;index++)
            {

                byte val = byteBuffer[index];

                if (status == 0)
                {

                    if (val == 0x11)
                    {
                        // 帧起始位1
                        status = 1;

                    }
                }
                else if (status == 1)
                {
                    if (val == 0x22)
                    {
                        // 帧起始位2
                        status = 2;
                    }
                    else
                    {
                        // 不是真正的帧头
                        status = 0;
                    }
                }
                else if (status == 2)
                {
                    // 高8位的帧长度
                    frameLength = val << 8;
                    status = 3;
                }
                else if (status == 3)
                {
                    // 低八位的帧长度
                    frameLength += val;
                    status = 4;
                    frame = new byte[frameLength];
                }
                else if (status == 4) {

                    // 帧内容
                    frame[frameIndex++] = val;

                    if (frameIndex == frameLength) {
                        status = 5;
                    }
                }

                else if (status == 5)
                {
                    if (val == 0x55)
                    {
                        // 帧结束位1
                        status = 6;
                    }
                    else {
                        // 异常：丢弃帧
                        status = 0;
                    }
                }
                else if (status == 6)
                {


                    if (val == 0x66)
                    {
                        // 帧结束位2
                        // 跳转帧开始
                        // 因为涉及 tcp 数据包粘包
                        // 粘包后，每收到一个完整数据包就会导致 虚拟调用链计数+1
                        // 为了避免这种情况，这里拷贝了一个调用链
                        new InvocationChain(invocationChain).invoke(frame);
                    }
                    /*else
                    {
                        // 还没有到达帧结尾
                        status = 4;
                    }*/
                    status = 0;
                }


                if (status == 0) {
                     frameIndex = 0;

                     frameLength = 0;
                }    
                    
            }
            
        }
    }
}
